package com.ifast.asset.controller;


import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.ifast.asset.domain.BaselineDO;
import com.ifast.asset.domain.BaselinedetailsDO;
import com.ifast.asset.service.BaselineService;
import com.ifast.asset.service.impl.BaselinedetailsServiceImpl;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ifast.common.annotation.Log;
import com.ifast.common.base.BaseController;
import com.ifast.common.utils.Result;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 
 * <pre>
 * 
 * </pre>
 * <small> 2021-11-22 14:43:46 | Aron</small>
 */
@Controller
@RequestMapping("/wxmp/baseline")
public class BaselineController extends BaseController {
	@Autowired
	private BaselineService baselineService;
	@Autowired
	BaselinedetailsServiceImpl baselinedetailsService;

	
	@ResponseBody
	@PostMapping("/list")
	public Result<Page<BaselineDO>> list(@RequestBody BaselineDO baselineDTO){
        Wrapper<BaselineDO> wrapper = new EntityWrapper<>(baselineDTO).orderBy("id", false);
        Page<BaselineDO> page = baselineService.selectPage(getPage(BaselineDO.class,baselineDTO.getPageNum(),baselineDTO.getPageSize()), wrapper);
        return Result.ok(page);
	}

	@ResponseBody
	@PostMapping("/save")
	public Result<String> save(@RequestBody BaselineDO baseline){

		baseline.setTime(new Date());
		baselineService.insert(baseline);
        return Result.ok();
	}

	@PostMapping( "/batchRemove")
	@ResponseBody
	public Result<String>  remove(@RequestBody Integer[] ids){
		baselineService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}

	//获取详情
	@PostMapping( "/getDetails")
	@ResponseBody
	public Result<Object>  getDetails(@RequestBody BaselinedetailsDO baselinedetailsDO){

		Wrapper wrapper = new EntityWrapper();
		Page<BaselineDO> page = baselinedetailsService.selectPage(getPage(BaselinedetailsDO.class,baselinedetailsDO.getPageNum(),baselinedetailsDO.getPageSize()), wrapper);
		return Result.ok(page);
	}

	@ResponseBody
	@GetMapping("/downLoadFile")
	public void downLoadExcle(HttpServletRequest request, HttpServletResponse response,String ip){

        String fileName = "baseline-CPoC_L3-CentOS7-System.csv";// 设置文件名，根据业务需要替换成要下载的文件名
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:SS");
        if (fileName != null) {
            //设置文件路径
            String realPath = "/opt/csv";
            File file = new File(realPath , fileName);
            if (file.exists()) {
                response.setContentType("application/vnd.ms-excel");//
                response.setHeader("content-type", "application/octet-stream");
                response.setHeader("Content-Disposition", "attachment;fileName=" + ip+"-"+simpleDateFormat.format(new Date())+".csv");// 设置文件名
                byte[] buffer = new byte[1024];
                FileInputStream fis = null;
                BufferedInputStream bis = null;
                try {
                    fis = new FileInputStream(file);
                    bis = new BufferedInputStream(fis);
                    OutputStream os = response.getOutputStream();
                    int i = bis.read(buffer);
                    while (i != -1) {
                        os.write(buffer, 0, i);
                        i = bis.read(buffer);
                    }
                    System.out.println("success");
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (bis != null) {
                        try {
                            bis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (fis != null) {
                        try {
                            fis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }

	}

}
